package com.zxh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import com.zxh.utils.DBUtils;
import com.zxh.entity.ResumeBasicInfo;

public class ResumeDao {
		public static ResumeBasicInfo isExistResme(int applicantId){
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs  = null;
			ResumeBasicInfo resume=new ResumeBasicInfo();
			String sql = "select * from tb_resume_basicinfo where applicantId=?";// 登录的sql语句
			try {
				con = DBUtils.getConnection(); // 获取数据库的连接
				ps = con.prepareStatement(sql);//执行预编译的 sql语句
				ps.setInt(1, applicantId);  //设置参数
				rs =  ps.executeQuery();  //执行查询
			
				while(rs.next()){
					resume.setApplicantId(rs.getInt("applicantId"));
					resume.setBasicinfoId(rs.getInt("basicinfoId"));
					resume.setRealName(rs.getString("realName"));
					resume.setGender(rs.getString("gender"));
					resume.setEmail(rs.getString("email"));
					resume.setBirthday(rs.getTimestamp("birthday"));
					resume.setCurrentLoc(rs.getString("currentLoc"));
					resume.setResidentLog(rs.getString("residentLog"));
					resume.setTelephone(rs.getString("TELEPHONE"));
					resume.setJobExperience(rs.getString("jobExperience"));
					resume.setJobIntension(rs.getString("jobIntension"));
					resume.setHeadShot(rs.getString("headShot"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				DBUtils.close(con, rs, null, ps);
			}
			return resume;
			
		}
		public int add(ResumeBasicInfo basicifno,int applicant_id){
			String sql="INSERT INTO tb_resume_basicinfo(applicantId, realName, gender, birthday, currentLoc, residentLog, telephone, email, jobIntension, jobExperience, headShot) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
			int basicinfoid =0;
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs  = null;
			try {
				con = DBUtils.getConnection(); // 获取数据库的连接
				ps = con.prepareStatement(sql);//执行预编译的 sql语句
				ps.setInt(1, applicant_id);
				ps.setString(2, basicifno.getRealName());
				ps.setString(3, basicifno.getGender());
				ps.setTimestamp(4, basicifno.getBirthday()==null?null : new Timestamp(basicifno.getBirthday().getTime()));
				ps.setString(5, basicifno.getCurrentLoc());
				ps.setString(6, basicifno.getResidentLog());
				ps.setString(7, basicifno.getTelephone());
				ps.setString(8, basicifno.getEmail());
				ps.setString(9, basicifno.getJobIntension());
				ps.setString(10, basicifno.getJobExperience());
				ps.setString(11, basicifno.getHeadShot());
				ps.executeUpdate();
				String sql2="select basicinfoId from tb_resume_basicinfo where applicantId=?";
				ps = con.prepareStatement(sql2);
				ps.setInt(1, applicant_id);
				rs = ps.executeQuery(); //执行查询
				if(rs.next()){
					basicinfoid = rs.getInt(1);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				DBUtils.close(con, rs, null, ps);
			}
			
			return basicinfoid;
		}
		
		public ResumeBasicInfo queryResuemBasicInfo(int basicInfo_id){
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs  = null;
			ResumeBasicInfo resume=new ResumeBasicInfo();
			String sql = "select * from tb_resume_basicinfo where basicinfoId=?";// 登录的sql语句
			try {
				con = DBUtils.getConnection(); // 获取数据库的连接
				ps = con.prepareStatement(sql);//执行预编译的 sql语句
				ps.setInt(1, basicInfo_id);  //设置参数
				rs =  ps.executeQuery();  //执行查询
			
				while(rs.next()){
					resume.setApplicantId(rs.getInt("applicantId"));
					resume.setBasicinfoId(rs.getInt("basicinfoId"));
					resume.setRealName(rs.getString("realName"));
					resume.setGender(rs.getString("gender"));
					resume.setEmail(rs.getString("email"));
					resume.setBirthday(rs.getTimestamp("birthday"));
					resume.setCurrentLoc(rs.getString("currentLoc"));
					resume.setResidentLog(rs.getString("residentLog"));
					resume.setTelephone(rs.getString("TELEPHONE"));
					resume.setJobExperience(rs.getString("jobExperience"));
					resume.setJobIntension(rs.getString("jobIntension"));
					resume.setHeadShot(rs.getString("headShot"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				DBUtils.close(con, rs, null, ps);
			}
			return resume;
			
		}
		public void updateHeadShot(int basicinfoId, String newFileName){
			String sql = "update tb_resume_basicinfo set headShot=? where basicinfoId= ?";//登录的sql语句
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs  = null;
			try {
				con = DBUtils.getConnection(); // 获取数据库的连接
				ps = con.prepareStatement(sql);//执行预编译的 sql语句
				ps.setString(1, newFileName);  //设置参数
				ps.setInt(2, basicinfoId);
				ps.executeUpdate();//执行更新操作
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally {
				DBUtils.close(con, rs, null, ps);
			}
			 
			
		}
		/**
		 * 修改简历基本信息
		 */		
		
		public void update(ResumeBasicInfo basicinfo, int basicInfo_id) {
			Connection con=null;
			PreparedStatement ps=null;
			String sql="update tb_resume_basicinfo set REALNAME=?,GENDER=?,BIRTHDAY=?," +
					"TELEPHONE=?,EMAIL=?,JOB_INTENSION=?,JOB_EXPERIENCE=? where BASICINFO_ID=?";
			try {
				con = DBUtils.getConnection(); // 获取数据库的连接
				ps=con.prepareStatement(sql);
				ps.setString(1,basicinfo.getRealName());
				ps.setString(2,basicinfo.getGender());
				ps.setTimestamp(3,basicinfo.getBirthday()==null?null:new Timestamp(basicinfo.getBirthday().getTime()));
				ps.setString(4, basicinfo.getTelephone());
				ps.setString(5, basicinfo.getEmail());
				ps.setString(6, basicinfo.getJobIntension());
				ps.setString(7, basicinfo.getJobExperience());
				ps.setInt(8,basicInfo_id);
				ps.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				DBUtils.close(con, null, null, ps);
			}
			
		}
}
		

